/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-24 下午6:23
 */

package com.lenovo.mso2o.ui.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lenovo.framework.base.BaseDialogFragment;
import com.lenovo.framework.base.BaseListAdapter;
import com.lenovo.mso2o.MSApplication;
import com.lenovo.mso2o.R;
import com.lenovo.mso2o.conf.MSConstants.FragmentType;
import com.lenovo.mso2o.entity.g.UploadLocation;
import com.lenovo.mso2o.entity.g.UploadLocationDao;
import com.lenovo.mso2o.ui.adapter.LocationConditionSelectAdapter;
import com.lenovo.mso2o.util.Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by lit on 2015/11/26.
 */
public class LocationChoiceConditionDialogFragment extends BaseDialogFragment{
    public static final String LOCATION_SELECTIONS = "location_selections";
    //private ArrayList<LocationItem> datas = new ArrayList<>();
    private List<UploadLocation> datas = new ArrayList<>();
    private BaseListAdapter adapter;
    private UploadLocationDao locationDao;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        locationDao = MSApplication.getDaoSession().getUploadLocationDao();
        //ArrayList<String> list = PrefExtraUtils.loadList(LOCATION_SELECTIONS);
        datas = locationDao.queryBuilder().list();
        Collections.reverse(datas);
        /*if(list == null)
            list = new ArrayList<>();
        for(UploadLocation loc : list)
            datas.add(loc.getLocation());*/
        UploadLocation locationItem = new UploadLocation();
        locationItem.setLocation(getActivity().getString(R.string.add_new_address));
        datas.add(locationItem);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View view = inflater.inflate(R.layout.choice_condition_dialogfragment, container);
        ListView orderContentChoiceCondition = (ListView) view.findViewById(R.id.order_content_choice_condition);
        adapter = new LocationConditionSelectAdapter(datas, getActivity());
        orderContentChoiceCondition.setAdapter(adapter);
        orderContentChoiceCondition.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position == adapter.getCount() - 1){
                    Bundle bundle = new Bundle();
                    bundle.putString("location", "ADD_NEW_ADDRESS");
                    mInterface.onRetrieveDialogFragmentData(bundle, FragmentType.DIALOG_LOCATION_CHOICE_FRAGMENT);
                    dismiss();
                    return;
                }
                Bundle bundle = new Bundle();
                UploadLocation location = (UploadLocation) adapter.getItem(position);
                bundle.putString("location", location.getLocation());
                bundle.putDouble("latitude", location.getLatitude());
                bundle.putDouble("longtitude", location.getLongtitude());
                mInterface.onRetrieveDialogFragmentData(bundle, FragmentType.DIALOG_LOCATION_CHOICE_FRAGMENT);
                dismiss();
            }
        });
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
//                        int downY = (int) event.getY() + outHeight * 2;
                        int downY = (int) event.getY();
                        if (isOutDistrict(downY, v)) {
                            dismiss();
                        }
                        break;
                }
                return false;
            }
        });
        return view;
    }

}
